//
//  ZDGoodsDetialController.m
//  ZDPension
//
//  Created by 刘甲奇 on 2017/6/15.
//  Copyright © 2017年 maker. All rights reserved.
//

#import "ZDGoodsDetialController.h"
#import "ZDGoodsInfoCell.h"
#import "ZDGoodsIntroduceCell.h"
#import "ZDGoodsBar.h"
#import "ZDGoodsDetailModel.h"

#import "ZDAlertController.h"
#import "ZDCommitListController.h"
#import "ZDShoppCarController.h"
#import "ZDSureOrderController.h"

static NSString *const ZDGoodsInfoCellId = @"ZDGoodsInfoCell";
static NSString *const ZDGoodsIntroduceCellId = @"ZDGoodsIntroduceCell";

@interface ZDGoodsDetialController ()

/**
 商品模型
 */
@property(nonatomic,strong) ZDGoodsDetailModel *detialModel;

@property (nonatomic,assign) int goodsNumber;
/**
 选择服务类型
 */
@property(nonatomic,strong)NSNumber *consumeType;

/**
 工具条
 */
@property(nonatomic,strong)ZDGoodsBar *goodsBar;

@end

@implementation ZDGoodsDetialController

- (void)setupUI
{
    [super setupUI];
    
    CGFloat goodsBarY = 0.0;
    //iPhone X
    if (LEScreenHeight == 812)
    {
        goodsBarY = LEScreenHeight -statusBarH -navigationBarH -homeBarH -65;
    }else{
        goodsBarY = LEScreenHeight -statusBarH -navigationBarH -65;
    }
    
    self.title = self.goodsName;
    [self.tableView registerNib:[UINib nibWithNibName:@"ZDGoodsInfoCell" bundle:nil] forCellReuseIdentifier:ZDGoodsInfoCellId];
    [self.tableView registerNib:[UINib nibWithNibName:@"ZDGoodsIntroduceCell" bundle:nil] forCellReuseIdentifier:ZDGoodsIntroduceCellId];
    [self resetTablewFrame:CGRectMake(0, 0, LEScreenWidth, goodsBarY)];
    
    ZDGoodsBar *goodsBar = (ZDGoodsBar *)[UIView getNibView:@"ZDGoodsBar"];
    goodsBar.frame = CGRectMake(0,goodsBarY, LEScreenWidth, 50);
    [self.view addSubview:goodsBar];
    [goodsBar.showShoppCarButton addTarget:self action:@selector(showShopClick) forControlEvents:UIControlEventTouchUpInside];
    [goodsBar.addShoppCarButton addTarget:self action:@selector(addShopClick) forControlEvents:UIControlEventTouchUpInside];
    self.goodsBar = goodsBar;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(selectConsumeType:) name:SELECT_PAYTYPE object:nil];
}
- (void)loadData
{
    self.goodsNumber = 1;
    [self requestRefreshData];
}
#pragma mark- 网络请求
- (void)requestRefreshData
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:self.goodsId forKey:@"goods_id"];
    
    [SVProgressHUD showWithStatus:@"正在加载中"];
    __weak typeof (self) weakSelf = self;
    [API netAccess:ServiceGoodsDetialUrl andMethor:@"GET" andDict:dict success:^(NSDictionary *successCode)
    {
        [SVProgressHUD dismiss];
        ZDLog(@"%s---%d---%@",__func__,__LINE__,successCode);
        NSDictionary *datas = successCode[@"datas"];
        NSString *error = datas[@"error"];
        if (error)
        {
            [weakSelf addNoDataViewWithTipText:error];
            [weakSelf.dataSource removeAllObjects];
        }else
        {
            [weakSelf hiddenNoDataView];
            NSString *imageUrl = datas[@"goods_image"];
            ZDGoodsDetailModel *model = [ZDGoodsDetailModel mj_objectWithKeyValues:datas[@"goods_info"]];
            model.goods_image_url = imageUrl;
            weakSelf.detialModel = model;
        }
        //设置工具条
        if ([weakSelf.detialModel.gc_id isEqualToString:@"2452"] ||[weakSelf.detialModel.gc_id isEqualToString:@"2828"])
        {
            [weakSelf.goodsBar.addShoppCarButton setTitle:@"立即订购" forState:0];
            
        }else{
            
        }
        [weakSelf.tableView reloadData];
        
    } falure:^(NSError *er) {
        ZDLog(@"%s---%d---%@",__func__,__LINE__,er);
        [SVProgressHUD dismiss];
    }];
}

/**
 请求加入购物车
 */
- (void)requestAddShopCar
{
    //door_service=0&goods_id=392&key=677e3d2374b67e5139fca5be70082508&quantity=4
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:self.goodsId forKey:@"goods_id"];
    [dict setObject:@(self.goodsNumber) forKey:@"quantity"];
    if ([self.consumeType isEqualToNumber:@(1)]) {
        [dict setObject:@(0) forKey:@"door_service"];
    }else
    {
        [dict setObject:@(1) forKey:@"door_service"];
    }
    [SVProgressHUD showWithStatus:@"正在加载中"];
    [API requestPostAppendKey:AddShopCarUrl params:dict success:^(NSDictionary *successCode) {
        ZDLog(@"%s---%d---%@",__func__,__LINE__,successCode);
        id datas = successCode[@"datas"];
        if ([datas isKindOfClass:[NSDictionary class]])
        {
            NSString *error = datas[@"error"];
            if (error)
            {
                [SVProgressHUD showErrorWithStatus:error];
            }
        }else if ([datas isKindOfClass:[NSString class]])
        {
            [SVProgressHUD showSuccessWithStatus:@"加入购物车成功"];
        }

    } falure:^(NSError *er) {
        
    }];
}

#pragma mark- tableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 1)
    {
        ZDGoodsIntroduceCell *cell = [tableView dequeueReusableCellWithIdentifier:ZDGoodsIntroduceCellId forIndexPath:indexPath];
        cell.model = self.detialModel;
        [cell.commitButton addTarget:self action:@selector(showAllCommit) forControlEvents:UIControlEventTouchUpInside];
        return cell;
    }
    ZDGoodsInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:ZDGoodsInfoCellId forIndexPath:indexPath];
    cell.model = self.detialModel;
     __weak typeof (self) weakSelf = self;
    [cell goodsNumberClick:^(int goodsNumber) {
        weakSelf.goodsNumber = goodsNumber;
        ZDLog(@"%s---%d---%d",__func__,__LINE__,goodsNumber);
    }];
    
    return cell;
}
#pragma mark- 点击事件
- (void)showShopClick
{
    ZDLog(@"%s---%d---去购物车",__func__,__LINE__);
    ZDShoppCarController *shopCar = [[ZDShoppCarController alloc] init];
    [self.navigationController pushViewController:shopCar animated:YES];

}
- (void)addShopClick
{
    ZDLog(@"%s---%d---加入购物车",__func__,__LINE__);
    if ([self.detialModel.gc_id isEqualToString:@"2452"] ||[self.detialModel.gc_id isEqualToString:@"2828"])
    {
        //下单
        ZDSureOrderController *sureOrder = [[ZDSureOrderController alloc] init];
        self.detialModel.goods_num = [NSString stringWithFormat:@"%d",self.goodsNumber];
        sureOrder.singalModel = self.detialModel;
        sureOrder.buyTpye = SingalBuy;
        CGFloat totalPrice = [self.detialModel.goods_price floatValue] * self.goodsNumber;
        sureOrder.totalPrice = [NSString stringWithFormat:@"总计: %.2f",totalPrice];
        [self.navigationController pushViewController:sureOrder animated:YES];
    }else
    {
        //加入购物车
        if ([self.detialModel.door_service isEqualToString:@"1"])
        {
            ZDAlertController *alertController = [[ZDAlertController alloc] init];
            [alertController showSheetView];
            [self presentViewController:alertController animated:YES completion:nil];
        }else
        {
            //直接加入购物车
            self.consumeType = @(1);
            [self requestAddShopCar];
        }

    }


}
- (void)selectConsumeType:(NSNotification *)notification
{
    
    NSNumber *number = notification.object;
    ZDLog(@"%s---%d---%@",__func__,__LINE__,number);

    self.consumeType = number;
    if ([number isEqualToNumber:@(0)])
    {
        //点击了取消
        return;
    }
    [self requestAddShopCar];
}
- (void)showAllCommit
{
    ZDLog(@"%s---%d---查看全部评价",__func__,__LINE__);
    ZDCommitListController *commit = [[ZDCommitListController alloc] init];
    commit.goodsId = self.goodsId;
    [self.navigationController pushViewController:commit animated:YES];
}

@end






